import java.util.LinkedList;
import java.util.Vector;

public class XMLParserState {
  public static String DUMMY_TRAIN_WORD_ = "DUMMY_TRAIN";
  public static String DUMMY_TEST_WORD_ = "DUMMY_TEST";
  public static int COLLOCATION_FEAT_LENGTH_ = 2;

  private String lexelt_;
  private String instanceId_;
  private Vector<String> senses_;
  private LinkedList<String> prevNWords_;
  private LinkedList<String> succNWords_;
  private Vector<String> bowContextWords_;
  private boolean inHead_;
  private int headCount_;

  private boolean isTrain_;

  public XMLParserState(boolean isTrain) {
    isTrain_ = isTrain;

    lexelt_ = "";
    senses_ = new Vector<String>();
    prevNWords_ = new LinkedList<String>();
    succNWords_ = new LinkedList<String>();
    bowContextWords_ = new Vector<String>();
    headCount_ = 0;
  }
  
  public XMLParserState(boolean isTrain, int featLength) {
    isTrain_ = isTrain;

    lexelt_ = "";
    senses_ = new Vector<String>();
    prevNWords_ = new LinkedList<String>();
    succNWords_ = new LinkedList<String>();
    bowContextWords_ = new Vector<String>();
    headCount_ = 0;
    COLLOCATION_FEAT_LENGTH_ = featLength;
  }
  
  public String getLexelt() {
    return lexelt_;
  }

  public void setLexelt(String lexelt) {
    lexelt_ = lexelt;
  }
  
  public String getInstanceId() {
    return instanceId_;
  }
  
  public void addToPrev(String word) {
    prevNWords_.add(word);
  }
  
  public void resetForInstance(String instanceId) {
    instanceId_ = instanceId;
    headCount_ = 0;
    bowContextWords_.clear();
    senses_.clear();
    succNWords_.clear();
    prevNWords_.clear();
    for (int i = 0; i < COLLOCATION_FEAT_LENGTH_; ++i) {
      if (isTrain_)
        prevNWords_.add(DUMMY_TRAIN_WORD_);
      else
        prevNWords_.add(DUMMY_TEST_WORD_);
    }
  }
  
  public void addBowContextWord(String word) {
    bowContextWords_.add(word);
  }
  
  public Vector<String> getContextWords() {
//    Vector<String> v = new Vector<String>();    
//    v.addAll(prevNWords_);
//    v.addAll(succNWords_);
//    return v;
    return bowContextWords_;
  }
  
  public void addCollocationWord(String word) {
    if (inHead_ == false) {
      if (headCount_ > 0) {
        addToSuccWord(word);
      } else {
        addToPrevWord(word);
      }
    }
  }
  
  public void addToPrevWord(String word) {
    prevNWords_.add(word);
    prevNWords_.remove();
  }
  
  public void addToSuccWord(String word) {
    if (succNWords_.size() < COLLOCATION_FEAT_LENGTH_) {
      succNWords_.add(word);
    }
  }
  
  public LinkedList<String> getPrevNList() {
    return prevNWords_;
  }
  
  public LinkedList<String> getSuccNList() {
    while (succNWords_.size() < COLLOCATION_FEAT_LENGTH_) {
      if (isTrain_) {
        succNWords_.add(DUMMY_TRAIN_WORD_);
      } else {
        succNWords_.add(DUMMY_TEST_WORD_);
      }
    }

    return succNWords_;
  }
  
  public boolean isInContext() {
    return !inHead_;
  }
  
  public void startHeadTag() {
    headCount_++;
    inHead_ = true;
  }

  public void endHeadTag() {
    inHead_ = false;
  }
  
  public void addSense(String senseId) {
    senses_.add(senseId);
  }
  
  public Vector<String> getSensesForInstance() {
    return senses_;
  }
}
